//
//  QNDividingRulerCell.m
//  QNDividingRuler
//
//  Created by rsh on 2019/12/7.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "QNDividingRulerCell.h"


@implementation QNDividingRulerModel

@end


@interface QNDividingRulerCell ()

@property (nonatomic,strong)UIView      *lineView;       //刻度线
@property (nonatomic,strong)UILabel     *titleLabel;     //刻度文本
@property (nonatomic,strong)UIImageView *mbImage;        //显示"面包"等的图片

@property (nonatomic,strong)UIView      *inPointView;

@end

@implementation QNDividingRulerCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


-(void)setupUI{
    self.lineView = [[UIView alloc]init];
    [self.contentView addSubview:self.lineView];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    self.mbImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.mbImage];
    [self.mbImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(30);
        make.centerX.equalTo(self.contentView);
        make.width.height.equalTo(@24);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.width.equalTo(@45);
        make.height.equalTo(@20);
        make.centerX.equalTo(self.contentView);
    }];
    
    self.inPointView = [[UIView alloc]init];
    [self.contentView addSubview:self.inPointView];
    [self.inPointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lineView.mas_top).offset(-10);
        make.width.height.equalTo(@6);
        make.centerX.equalTo(self.lineView.mas_centerX);
    }];
}


-(void)setModel:(QNDividingRulerModel *)model{
    [self.lineView setBackgroundColor:model.isLargeLine?model.largeLineColor:model.smallLineColor];
    CGFloat lineWidth  = model.isLargeLine?model.lineWidth:model.lineWidth;
    CGFloat lineHeight = model.isLargeLine?model.largeLineHeight:model.smallLineHeight;
    CGFloat bottomOffset = model.isLargeLine?5:(model.largeLineHeight-model.smallLineHeight)/2+5;
//    NSLog(@"=====---%f",bottomOffset);
    bottomOffset = model.currentRulerAlignment == QNRulerTypeAlignmentCenter?bottomOffset:5;
//    NSLog(@"---%f,---%f,---%f",lineHeight,lineWidth,bottomOffset);
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(self.contentView.mas_bottom).offset(-bottomOffset);
        make.top.equalTo(self.contentView.mas_top).offset(74);
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.width.equalTo(@(lineWidth));
        make.height.equalTo(@(lineHeight));
    }];
}


-(void)updateCellWithText:(NSString *)showText fontFloat:(CGFloat)fontFloat textColor:(UIColor *)textColor textLargeLineSpace:(CGFloat)space isSelected:(BOOL)isSelected{
  
    NSDictionary *attributes  = [NSDictionary dictionaryWithObject:[UIFont fontWithName:ScaleDisLableFontName size:fontFloat] forKey:NSFontAttributeName];
    CGSize showTextSize = [showText sizeWithAttributes:attributes];
    
    if (showText || ![showText isEqualToString:@""]) {
        [self.titleLabel setHidden:NO];
        [self.titleLabel setText:showText];
    }else{
        [self.titleLabel setHidden:YES];
    }

    if (isSelected) {
        [self.titleLabel setTextColor:[UIColor qn_colorWithHexString:QNMaiColor]];
        self.titleLabel.font = [UIFont fontWithName:ScaleSelLableFontName size:14];
    }else{
        [self.titleLabel setTextColor:textColor];
        self.titleLabel.font = [UIFont fontWithName:ScaleDisLableFontName size:14];
    }
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(space);
        make.width.equalTo(@(showTextSize.width+2));
        make.height.equalTo(@(showTextSize.height));
        make.centerX.equalTo(self.contentView);
    }];
    
}

-(void)updateCellinPointColor:(UIColor *)color pointWH:(CGFloat)wh largeSpace:(CGFloat)space isShow:(BOOL)isShow{
    [self.inPointView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(wh));
        make.centerX.equalTo(self.lineView.mas_centerX);
        make.bottom.equalTo(self.lineView.mas_top).offset(-space);
    }];
    
    [self.inPointView setHidden:!isShow];
    [self.inPointView setBackgroundColor:color];
    [self.inPointView.layer setCornerRadius:wh/2];
    [self.inPointView.layer setMasksToBounds:YES];
}


-(void)updateCellWithImage:(NSString *)imageStr
{
    if (imageStr || ![imageStr isEqualToString:@""]) {
        [self.mbImage setHidden:NO];
        if ([imageStr containsString:@"http://"] ||[imageStr containsString:@"https://"]) {
            //网络加载
            [self.mbImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:nil];
        }else{
            //本地加载
            self.mbImage.image = [UIImage imageNamed:imageStr];
        }
    }else{
        [self.mbImage setHidden:YES];
    }
}


@end
